
const SHOUTCAST_DEVID = "st1LOEUzorO6xySw";
const MAIN_API_URL = "http://api.shoutcast.com/";
const TUNEIN_LINK = "http://yp.shoutcast.com/sbin/tunein-station.pls";

var Shoutcast = {
  _backGround: null,
  
  /**
   * Initialize function for this object.
   */
  init: function() {
    // Get our background page
    this._backGround = chrome.extension.getBackgroundPage();

    // First get the genres
    this.getGenres();
    // By default we show a selection of random stations
    this.getTop500(5);
  },

  /**
   * Queries the list of valid genres from the shoutcast api. Then fills the
   * option box with the list of genres for the user to select.
   */
  getGenres: function() {
    var url = MAIN_API_URL + "genre/primary?k=" + SHOUTCAST_DEVID + "&f=json";
    var req = new XMLHttpRequest();
    req.open("GET", url, true);
    req.onreadystatechange = function() {
      if (req.readyState == 4) {
        var jsObj = JSON.parse(req.responseText).response
                                                .data
                                                .genrelist
                                                .genre;
        var genreSelectList = domGetElementById(document, "genre_select");
        jsObj = [ { id: 1, name: "Test1", parentid: 0},
                  { id: 2, name: "Test2", parentid: 0},
                  { id: 3, name: "Test3", parentid: 0},
                  { id: 4, name: "Test4", parentid: 0},
                  { id: 5, name: "Test5", parentid: 0} ];
        jstProcess(new JsEvalContext(jsObj), genreSelectList, true);
      }
    };

    req.send(null);
  },

  /**
   * Gets a list of random stations.
   */
  getRandomStations: function(numStations) {
    var url = MAIN_API_URL + "station/randomstations?k=" + SHOUTCAST_DEVID +
                "&limit=" + (numStations > 0 ? numStations : 1) + "&f=json";
    var req = new XMLHttpRequest();
    req.open("GET", url, true);
    req.onreadystatechange = function() {
      if (req.readyState == 4) {
        var stationBlock = document.getElementById("stations");
        var jsObj = JSON.parse(req.responseText).response
                                                .data
                                                .stationlist
                                                .station;
        jstProcess(new JsEvalContext(jsObj), stationBlock);
      }
    };

    req.send(null);
  },

  getTop500: function(limit) {
    var url = MAIN_API_URL + "legacy/Top500?k=" + SHOUTCAST_DEVID + "&limit=5";
    var req = new XMLHttpRequest();
    req.open("GET", url, true);
    req.onreadystatechange = function() {
      if (req.readyState == 4) {
        var stationBlock = document.getElementById("stations");
        jsObj = [];
        
        // We have to convert the XML to a JS Object
        var xmlStationList = req.responseXML.getElementsByTagName("station");
        for (var i=0, xmlStation; xmlStation = xmlStationList[i]; i++) {
          jsObj.push(
            {
              id: xmlStation.getAttribute("id"),
              name: xmlStation.getAttribute("name"),
              mt: xmlStation.getAttribute("mt"),
              br: xmlStation.getAttribute("br"),
              genre: xmlStation.getAttribute("genre"),
              ct: xmlStation.getAttribute("ct"),
              lc: xmlStation.getAttribute("lc"),
              alternate: (i % 2 == 0 ? "odd" : "even")
            });
        }
        jstProcess(new JsEvalContext(jsObj), stationBlock);
      }
    };

    req.send(null);
  },

  loadStation: function(jstdata) {
    var url = TUNEIN_LINK + "?id=" + jstdata.id;
    var req = new XMLHttpRequest();
    var self = this;
    req.open("GET", url, true);
    req.onreadystatechange = function() {
      if (req.readyState == 4) {
        console.log(req.responseText); // Hmm, why won't this get the pls file?
        //self._backGround.ChromecastPlayer.Play("http://76.73.4.30:80"); //;stream.nsv
        var url = "http://scfire-mtc-aa05.stream.aol.com:80/stream/1074;stream.nsv";
        var audio = new Audio(url);
        console.log("Playing url: " + url);
        audio.play();
      }
    };
    
    req.send(null);
  },

  nothing: function(){
    
  }
}